package com.simpleframe.web.action;

import java.io.File;
import java.io.FileInputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import com.simpleframe.util.Assert;
import com.simpleframe.util.DateUtil;

public class SingleFileUploadAction extends BaseFileUploadAction{

	private static final long serialVersionUID = 1L;
	
	protected File file;// 上传文件域对象
	protected String fileName;// 上传文件名
	protected String saveFileName;//从新命名后的文件名
	protected String ContentType;// 上传文件类型
	protected String savePath;// 保存文件的目录路径
	
	
	/**
     *   获取指定文件的扩展名
     *  
     *   @param   f
     *   @return
     */
   protected String getFileExtension(String fileName)   {
	   if(fileName!=null){
           int i = fileName.lastIndexOf('.');
           if(i>0 && i<fileName.length()-1)   {
        	   return fileName.substring(i+1).toLowerCase();
           }
	   }
	   return  null;
   } 
	
	/**
	 * 以时间命名
	 * @param savePath
	 * @return
	 */
   protected String getDstFileName(String fileName){
		String day = DateUtil.get("yyyyMMddHHmmSSS");
		int random = (int)(Math.random()*9000)+1000;
		return day + random+"."+ getFileExtension(fileName);
	}
   
   protected String getDstFileName(String name, String fileName ){
	   String day = DateUtil.get("ddHHmmSSS");
		int random = (int)(Math.random()*9000)+1000;
		return name + "_" + day + random+"."+ getFileExtension(fileName);
   }
	
	/**
	 * 新建文件夹
	 * @param savePath
	 */
   protected void newDateFolder(String savePath){
		String path = savePath;
		File file = new File(path);
		if(!file.exists()){
			file.mkdir();
		}
	}
	
	/**
	 * 得到当天时间，用于命名文件夹
	 * @return
	 */
	protected String getIntraday(){
		SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
		Calendar c = Calendar.getInstance();
		return format.format(c.getTime());
	}
	
	/**
	 * 得到当月，用于命名文件夹
	 * @return
	 */
	protected static String getIntraMonth(){
		SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
		Calendar c = Calendar.getInstance();
		return format.format(c.getTime());
	}
	
	/**
	 * 以逗号隔开 如isAllowType("jpg|sdf|sdf|sdf|sdf,")
	 * @param types
	 * @return
	 */
	protected boolean isAllowType(String types){
		return isAllowType(getFileExtension(fileName), types);
	}
	
	/**
	 * 验证是否是允许上传的文件
	 * 以逗号隔开 如isAllowType("jsp","jpg|sdf|sdf|sdf|sdf")
	 * @param ext
	 * @param types
	 * @return
	 */
	protected boolean isAllowType(String ext, String types){
		Assert.notEmpty(ext);
		Assert.notEmpty(types);
		String[] typeArr = types.replaceAll(" ", "").split("\\|");
		ext = ext.replaceAll(" ", "");
		for (String type : typeArr) {
			if(ext.equalsIgnoreCase(type)){
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 文件大小是否在 a b之间， K 为单位
	 * @return
	 */
	public boolean isSize(double a, double b){
		FileInputStream fis = null;
        try{
            fis = new FileInputStream(file);  
            double size = fis.available()/1000;
            if(size>=a && size<=b){
            	return true;
            }
        }catch(Exception e){   
           e.printStackTrace();
        }
        return false;
	}
	
	public static void main(String[] args) {
		SingleFileUploadAction singleFileUploadAction = new SingleFileUploadAction();
		System.out.println(singleFileUploadAction.isAllowType("apk", "apk"));
	}
	
	/**
	 * 返回上传成功结果
	 * @param state
	 * @param uri
	 * @param msg
	 */
	protected void result(String state, String uri, String msg){
		renderHtml("{ \"state\": \""+state+"\", \"uri\":\""+uri+"\", \"message\": \""+msg+"\" } ");
	}
	
    
    public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileName() {
		return fileName;
	}
	
	public void setFileName(String fileName){
		this.fileName = fileName;
	}

	public void setFileFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getContentType() {
		return ContentType;
	}

	public void setContentType(String contentType) {
		ContentType = contentType;
	}

	public String getSavePath() {
		return savePath;
	}
	
	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}
	
	public void setSaveFileName(String saveFileName){
		this.saveFileName = saveFileName;
	}
	
	public String getSaveFileName(){
		return saveFileName;
	}
	
}
